package net.guerlab.sdk.anubis.helper;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

import net.guerlab.sdk.anubis.AnubisException;
import net.guerlab.sdk.anubis.entity.NotifyInfo;

/**
 * 通知助手
 * 
 * @author guer
 *
 */
public class NotifyHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotifyHelper.class);

    protected ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 构造默认通知助手
     */
    public NotifyHelper() {
        super();
    }

    /**
     * 通过objectMapper构造通知助手
     * 
     * @param objectMapper
     *            objectMapper
     */
    public NotifyHelper(
            ObjectMapper objectMapper) {
        super();
        this.objectMapper = objectMapper;
    }

    /**
     * 解析通知信息
     * 
     * @param body
     *            消息体内容
     * @return 通知信息
     */
    public NotifyInfo parse(
            String body) {
        String trimBody = StringUtils.trim(body);

        if (StringUtils.isBlank(trimBody) || trimBody.length() <= 5) {
            return null;
        }

        String content = trimBody.substring(5);

        String jsonString = URLHelper.decode(content);

        try {
            return objectMapper.readValue(jsonString, NotifyInfo.class);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage(), e);
            throw new AnubisException(e.getMessage(), e);
        }
    }

    /**
     * 解析通知信息(新)
     * 
     * @param body
     *            消息体内容
     * @return 通知信息
     */
    public NotifyInfo parseV2(
            String body) {
        NotifyData data = null;
        try {
            data = objectMapper.readValue(body, NotifyData.class);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage(), e);
            throw new AnubisException(e.getMessage(), e);
        }

        String jsonString = URLHelper.decode(data.getData());

        try {
            return objectMapper.readValue(jsonString, NotifyInfo.class);
        } catch (Exception e) {
            LOGGER.debug(e.getMessage(), e);
            throw new AnubisException(e.getMessage(), e);
        }
    }

    public static class NotifyData {

        @JsonProperty("app_id")
        private String appId;

        private String data;

        private String salt;

        private String signature;

        /**
         * 返回 appId
         *
         * @return appId
         */
        public String getAppId() {
            return appId;
        }

        /**
         * 设置appId
         *
         * @param appId
         *            appId
         */
        public void setAppId(
                String appId) {
            this.appId = appId;
        }

        /**
         * 返回 data
         *
         * @return data
         */
        public String getData() {
            return data;
        }

        /**
         * 设置data
         *
         * @param data
         *            data
         */
        public void setData(
                String data) {
            this.data = data;
        }

        /**
         * 返回 salt
         *
         * @return salt
         */
        public String getSalt() {
            return salt;
        }

        /**
         * 设置salt
         *
         * @param salt
         *            salt
         */
        public void setSalt(
                String salt) {
            this.salt = salt;
        }

        /**
         * 返回 signature
         *
         * @return signature
         */
        public String getSignature() {
            return signature;
        }

        /**
         * 设置signature
         *
         * @param signature
         *            signature
         */
        public void setSignature(
                String signature) {
            this.signature = signature;
        }
    }

    /**
     * 返回 objectMapper
     *
     * @return objectMapper
     */
    public ObjectMapper getObjectMapper() {
        return objectMapper;
    }

    /**
     * 设置objectMapper
     *
     * @param objectMapper
     *            objectMapper
     */
    public void setObjectMapper(
            ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }
}
